package bb.lanxing.lib.devices.bryton.bbcp;

//import com.parse.codec.CharEncoding;
//import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;

public class BbcpData implements BbcpPackage {
    public static final int MAX_DATA_PAYLOAD = 18;
    public static final int SEQNO_BYTES = 2;
    public static final int START_FRAGMENT_LENGTH = 7;
    protected short mSeqNo;
    protected byte[] payload;
    protected byte type;

    public static byte[] intToBytes(int paramInt) {
//        byte[] bArr = {(byte) (r3 >> 8), (byte) (r3 & 255), (byte) (r3 & 255), (byte) (i & 255)};
//        int i2 = i >> 8;
//        int i3 = i2 >> 8;
//        return bArr;
        byte b1 = (byte)(paramInt & 0xFF);
        paramInt >>= 8;
        byte b2 = (byte)(paramInt & 0xFF);
        paramInt >>= 8;
        byte b3 = (byte)(paramInt & 0xFF);
        return new byte[] { (byte)(paramInt >> 8), b3, b2, b1 };
    }

    public static BbcpData createFragment(byte[] bArr, boolean z) throws BbcpValidationException {
        if (bArr.length < 2) {
            throw new BbcpValidationException("invalid length");
        }
        if (toShort(bArr, 0) == 0) {
            if (bArr.length < 7) {
                throw new BbcpValidationException("invalid length");
            }
            return new BbcpDataStartFragment(bArr);
        } else if (z) {
            BbcpData bbcpData = new BbcpData((byte) 1);
            bbcpData.unpack(bArr);
            return bbcpData;
        } else {
            BbcpData bbcpData2 = new BbcpData((byte) 2);
            bbcpData2.unpack(bArr);
            return bbcpData2;
        }
    }

    public static BbcpData createStartFragment(short s, byte b, byte b2, byte b3) {
        return new BbcpDataStartFragment(s, b, b2, b3);
    }

    public static BbcpData createEndFragment(short s, byte[] bArr) {
        return new BbcpData((byte) 1, s, getChecksum(bArr));
    }

    public static BbcpData createDataFragment(short s, byte[] bArr) {
        return new BbcpData((byte) 2, s, bArr);
    }

    public BbcpData(byte b, short s, byte[] bArr) {
        this.type = b;
        this.mSeqNo = s;
        this.payload = bArr;
    }


    public BbcpData(byte b) {
        this.type = b;
    }

    public static byte[] getChecksum(byte[] bArr) {
        long j = 0;
        for (byte b : bArr) {
            j += b & 255;
        }
        return intToBytes((int) (j & (-1)));
    }

    public static int toInt(byte[] bArr, int i) {
        return (bArr[i + 3] & 255) | ((bArr[i] & 255) << 24) | ((bArr[i + 1] & 255) << 16) | ((bArr[i + 2] & 255) << 8);
    }

    public static short toShort(byte[] bArr, int i) {
        return (short) ((bArr[i + 1] & 255) | ((bArr[i] & 255) << 8));
    }

    public static void shortToBytes(short s, byte[] bArr, int i) {
        bArr[i + 1] = (byte) (s & 255);
        bArr[i] = (byte) (s >> 8);
    }

    public static byte[] packAsciiPayload(TreeMap<String, String> treeMap) throws BbcpValidationException {
        ByteBuffer allocate = ByteBuffer.allocate(512);
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            if (allocate.position() >= 512) {
                throw new BbcpValidationException("ascii payload is more than 512 bytes");
            }
            allocate.put((entry.getKey() + entry.getValue()).getBytes(StandardCharsets.US_ASCII/*CharEncoding.US_ASCII*/));
            allocate.put((byte) 0);
        }
        allocate.position();
        allocate.flip();
        byte[] bArr = new byte[allocate.limit()];
        allocate.get(bArr);
        return bArr;
    }

    public static TreeMap<String, String> unpackAsciiPayload(byte[] bArr) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        int i = 0;
        for (int i2 = 0; i2 < bArr.length; i2++) {
            if (bArr[i2] == 0) {
                int i3 = i2 - i;
                if (i3 < 3) {
                    break;
                }
                byte[] bArr2 = new byte[i3];
                System.arraycopy(bArr, i, bArr2, 0, i3);
                String str = new String(bArr2);
                treeMap.put(str.substring(0, 2), str.substring(2));
                i = i2 + 1;
            }
        }
        return treeMap;
    }

    @Override
    public byte[] pack() throws BbcpValidationException {
        int i = 2;
        byte[] bArr = new byte[this.type == 1 ? this.payload.length + 2 + 1 : this.payload.length + 2];
        shortToBytes(this.mSeqNo, bArr, 0);
        if (this.type == 1) {
            bArr[2] = 0;
            i = 3;
        }
        byte[] bArr2 = this.payload;
        System.arraycopy(bArr2, 0, bArr, i, bArr2.length);
        return bArr;
    }

    @Override
    public void unpack(byte[] bArr) throws BbcpValidationException {
        if (bArr.length <= 2) {
            throw new BbcpValidationException("invalid length");
        }
        this.mSeqNo = toShort(bArr, 0);
        int length = this.type == 1 ? bArr.length - 3 : bArr.length - 2;
        byte[] bArr2 = new byte[length];
        this.payload = bArr2;
        System.arraycopy(bArr, bArr.length - length, bArr2, 0, bArr2.length);
    }

    public byte getType() {
        return this.type;
    }

    public short getSeqNo() {
        return this.mSeqNo;
    }

    public byte[] getPayload() {
        return this.payload;
    }
}
